<%@ include file='DatabaseSetup.jsp'%>
<%@ page import="java.text.DecimalFormat" %>
    <%-- AJAX display--%>

    <%-- Extract DB data --%>
    <%
        String userID = request.getParameter("uid");
        String puzzleID = request.getParameter("pid");

        DecimalFormat twoDForm = new DecimalFormat("#.##");

        /* output the puzzle being check */
        if(Integer.parseInt(puzzleID)!=0){
            int NUM_CELLS_IN_UNIT = 9;

            String sql = "select * from gamepuzzles where puzzleID=" +puzzleID;
            ResultSet rs = stmt.executeQuery(sql);

            while(rs.next()){
                String PuzzleID = rs.getString("PuzzleID");
                String NumGivenCells = rs.getString("NumGivenCells");
                String DefiniteValue1= rs.getString("DefiniteValue1");
                String DefiniteValue2= rs.getString("DefiniteValue2");
                String DefiniteValue3= rs.getString("DefiniteValue3");
                String DefiniteValue4= rs.getString("DefiniteValue4");
                String DefiniteValue5= rs.getString("DefiniteValue5");
                String DefiniteValue6= rs.getString("DefiniteValue6");
                String DefiniteValue7= rs.getString("DefiniteValue7");
                String DefiniteValue8= rs.getString("DefiniteValue8");
                String DefiniteValue9= rs.getString("DefiniteValue9");

                int[] CellValue= new int[NUM_CELLS_IN_UNIT*NUM_CELLS_IN_UNIT];
                for (int i=0; i<CellValue.length; i++){
                    CellValue[i]=0;
                }

                String[][] strResult = new String[NUM_CELLS_IN_UNIT][];
                strResult[0] = DefiniteValue1.trim().split(",");
                strResult[1] = DefiniteValue2.trim().split(",");
                strResult[2] = DefiniteValue3.trim().split(",");
                strResult[3] = DefiniteValue4.trim().split(",");
                strResult[4] = DefiniteValue5.trim().split(",");
                strResult[5] = DefiniteValue6.trim().split(",");
                strResult[6] = DefiniteValue7.trim().split(",");
                strResult[7] = DefiniteValue8.trim().split(",");
                strResult[8] = DefiniteValue9.trim().split(",");

                for(int i=0; i<NUM_CELLS_IN_UNIT; i++){
                    for(int j=0; j<strResult[i].length; j++){
                        CellValue[Integer.parseInt(strResult[i][j].trim())-1] = i+1;
                    }
                }

                out.print("<table class=\"sudoku\">");
                out.print("<tr><td class=\"title\"></td><td class=\"title\">A</td><td class=\"title\">B</td>" +
                        "<td class=\"title\">C</td><td class=\"title\">D</td><td class=\"title\">E</td>" +
                        "<td class=\"title\">F</td class=\"title\"><td class=\"title\">G</td><td class=\"title\">H</td>" +
                        "<td class=\"title\">I</td></tr>");
                for(int i=0; i<NUM_CELLS_IN_UNIT; i++){
                    out.print("<tr>");
                    out.print("<td class=\"title\">"+Integer.toString(i+1)+"</td>");
                    for(int j=0; j<NUM_CELLS_IN_UNIT; j++){
                        int k = i*NUM_CELLS_IN_UNIT + j;
                        if(CellValue[k]==0){
                            out.print("<td></td>");
                        }else{
                            out.print("<td class=\"given\">");
                            out.print(Integer.toString(CellValue[k]));
                            out.print("</td>");
                        }
                    }
                    out.print("</tr>");
                }
                out.print("</table>");
            }

            /* Count the number of opportunity to practice the rule */
            double SingleInGroup = 0;
            double NakedSingle = 0;
            double HiddenSingle = 0;
            double IntersectionReduction = 0;
            double NakedPairs = 0;
            double HiddenPairs = 0;
            double TotalRules = 0;

            sql = "select Count(*) from gameskillsmatrix where userID = "
                + userID + " and puzzleID = " + puzzleID + " and SingleInGroup='1'";
            rs = stmt.executeQuery(sql);
            rs.next();
            SingleInGroup = Double.parseDouble(rs.getString(1));

            sql = "select Count(*) from gameskillsmatrix where userID = "
                + userID + " and puzzleID = " + puzzleID + " and NakedSingle='1'";
            rs = stmt.executeQuery(sql);
            rs.next();
            NakedSingle = Double.parseDouble(rs.getString(1));

            sql = "select Count(*) from gameskillsmatrix where userID = "
                + userID + " and puzzleID = " + puzzleID + " and HiddenSingle='1'";
            rs = stmt.executeQuery(sql);
            rs.next();
            HiddenSingle = Double.parseDouble(rs.getString(1));

            sql = "select Count(*) from gameskillsmatrix where userID = "
                + userID + " and puzzleID = " + puzzleID + " and IntersectionReduction='1'";
            rs = stmt.executeQuery(sql);
            rs.next();
            IntersectionReduction = Double.parseDouble(rs.getString(1));

            sql = "select Count(*) from gameskillsmatrix where userID = "
                + userID + " and puzzleID = " + puzzleID + " and NakedPairs='1'";
            rs = stmt.executeQuery(sql);
            rs.next();
            NakedPairs = Double.parseDouble(rs.getString(1));

            sql = "select Count(*) from gameskillsmatrix where userID = "
                + userID + " and puzzleID = " + puzzleID + " and HiddenPairs='1'";
            rs = stmt.executeQuery(sql);
            rs.next();
            HiddenPairs = Double.parseDouble(rs.getString(1));

            TotalRules = SingleInGroup + NakedSingle + HiddenSingle +
                    IntersectionReduction + NakedPairs + HiddenPairs;

            /* Count the number of rules applied successfully */
            double SingleInGroupApplied = 0;
            double NakedSingleApplied = 0;
            double HiddenSingleApplied = 0;
            double IntersectionReductionApplied = 0;
            double NakedPairsApplied = 0;
            double HiddenPairsApplied = 0;
            double TotalRulesApplied = 0;

            sql = "select Count(*) from gamesolvingsteps where userID = "
                + userID + " and puzzleID = " + puzzleID + " and SingleInGroup='1' and AnswerHint='0'";
            rs = stmt.executeQuery(sql);
            rs.next();
            SingleInGroupApplied = Double.parseDouble(rs.getString(1));

            sql = "select Count(*) from gamesolvingsteps where userID = "
                + userID + " and puzzleID = " + puzzleID + " and NakedSingle='1' and AnswerHint='0'";
            rs = stmt.executeQuery(sql);
            rs.next();
            NakedSingleApplied = Double.parseDouble(rs.getString(1));

            sql = "select Count(*) from gamesolvingsteps where userID = "
                + userID + " and puzzleID = " + puzzleID + " and HiddenSingle='1' and AnswerHint='0'";
            rs = stmt.executeQuery(sql);
            rs.next();
            HiddenSingleApplied = Double.parseDouble(rs.getString(1));

            sql = "select Count(distinct gridID) from gameelmincellentries where userID = "
                + userID + " and puzzleID = " + puzzleID + " and IntersectReduct='1' and GivenByHintsFlg='0'";
            rs = stmt.executeQuery(sql);
            rs.next();
            IntersectionReductionApplied = Double.parseDouble(rs.getString(1));

            sql = "select Count(distinct gridID) from gameelmincellentries where userID = "
                + userID + " and puzzleID = " + puzzleID + " and NakedPairs='1' and GivenByHintsFlg='0'";
            rs = stmt.executeQuery(sql);
            rs.next();
            NakedPairsApplied = Double.parseDouble(rs.getString(1));

            sql = "select Count(distinct gridID) from gameelmincellentries where userID = "
                + userID + " and puzzleID = " + puzzleID + " and HiddenPairs='1' and GivenByHintsFlg='0'";
            rs = stmt.executeQuery(sql);
            rs.next();
            HiddenPairsApplied = Double.parseDouble(rs.getString(1));

            TotalRulesApplied = SingleInGroupApplied + NakedSingleApplied + HiddenSingleApplied +
                    IntersectionReductionApplied + NakedPairsApplied + HiddenPairsApplied;

            /* Find the total number of hints Used */
            int rowHints = 0;
            int colHints = 0;
            int rulesHints = 0;
            int answerHints = 0;

            sql = "select Count(*) from gamesolvingsteps where userID = "
                + userID + " and puzzleID = " + puzzleID + " and rowHint = '1'";
            rs = stmt.executeQuery(sql);
            rs.next();
            rowHints = Integer.parseInt(rs.getString(1));

            sql = "select Count(*) from gamesolvingsteps where userID = "
                + userID + " and puzzleID = " + puzzleID + " and colHint = '1'";
            rs = stmt.executeQuery(sql);
            rs.next();
            colHints = Integer.parseInt(rs.getString(1));

            sql = "select Count(*) from gamesolvingsteps where userID = "
                + userID + " and puzzleID = " + puzzleID + " and rulesHint = '1'";
            rs = stmt.executeQuery(sql);
            rs.next();
            rulesHints = Integer.parseInt(rs.getString(1));

            sql = "select Count(*) from gamesolvingsteps where userID = "
                + userID + " and puzzleID = " + puzzleID + " and answerHint = '1'";
            rs = stmt.executeQuery(sql);
            rs.next();
            answerHints = Integer.parseInt(rs.getString(1));

            /* get the total time to solve the puzzle */
            int elapsedTime = 0;

            sql = "select ElapsedTime from gamesolvingsteps where userID = "
                + userID + " and puzzleID = " + puzzleID + " order by StepID desc";
            rs = stmt.executeQuery(sql);
            rs.next();
            elapsedTime = Integer.parseInt(rs.getString(1));

            String SingleInGroupPrecent = "";
            String NakedSinglePrecent = "";
            String HiddenSinglePrecent = "";
            String IntersectionReductionPrecent = "";
            String NakedPairsPrecent = "";
            String HiddenPairsPrecent = "";

            if(SingleInGroup!=0){
                SingleInGroupPrecent = twoDForm.format(SingleInGroupApplied/SingleInGroup*100);
            }else{
                SingleInGroupPrecent = "N/A";
            }
             if(NakedSingle!=0){
                NakedSinglePrecent = twoDForm.format(NakedSingleApplied/NakedSingle*100);
            }else{
                NakedSinglePrecent = "N/A";
            }
            if(HiddenSingle!=0){
                HiddenSinglePrecent = twoDForm.format(HiddenSingleApplied/HiddenSingle*100);
            }else{
                HiddenSinglePrecent = "N/A";
            }
            if(IntersectionReduction!=0){
                IntersectionReductionPrecent = twoDForm.format(IntersectionReductionApplied/IntersectionReduction*100);
            }else{
                IntersectionReductionPrecent = "N/A";
            }
            if(NakedPairs!=0){
                NakedPairsPrecent = twoDForm.format(NakedPairsApplied/NakedPairs*100);
            }else{
                NakedPairsPrecent = "N/A";
            }
            if(HiddenPairs!=0){
                HiddenPairsPrecent = twoDForm.format(HiddenPairsApplied/HiddenPairs*100);
            }else{
                HiddenPairsPrecent = "N/A";
            }

            /* find the misconception that made by user*/
            sql = "select misconception from usermisconception where userID = "
                    + userID + " and puzzleID = " + puzzleID ;
            rs = stmt.executeQuery(sql);
            rs.next();
            double misconception = Double.parseDouble(rs.getString(1));

            String misconceptionPrecent ="";
            if (TotalRulesApplied !=0 ){
                misconceptionPrecent = twoDForm.format(misconception/TotalRulesApplied*100);
            }
            else{
                misconceptionPrecent = "N/A";
            }

            rs.close();
            con.close();
            stmt.close();
        
    %>

    <%-- output puzzle information --%>

    <br/>

    <table name="gamestatic" class ="gamestatic">
        <tr>
            <td class="header">Puzzle ID : </td><td colspan="2"><%=puzzleID%></td>
        </tr>

        <tr class ="even">
            <td class="header">Elapsed Time : </td><td colspan="2"><%=elapsedTime%> second(s)</td>
        </tr>

        <tr>
            <td class="header" rowspan="4">Number of Hints Used : </td>
            <td>Row Hints</td><td><%=rowHints%></td>
        </tr>
        <tr>
            <td valign="top">Column Hints</td><td><%=colHints%></td>
        </tr>
        <tr>
            <td valign="top">Rules Hints</td><td><%=rulesHints%></td>
        </tr>
        <tr>
            <td valign="top">Answer Hints</td><td><%=answerHints%></td>
        </tr>

        <tr class="even">
            <td class="header" rowspan="6">Rules Applied Successfully:<br/> %(Number) </td><td>Single In Group</td><td><%= SingleInGroupPrecent%>% (<%=SingleInGroupApplied%>) </td>
        </tr>
        <tr class="even">
            <td valign="top">Naked Single</td><td><%= NakedSinglePrecent%>% (<%=NakedSingleApplied%>) </td>
        </tr>
        <tr class="even">
            <td valign="top">Hidden Single</td><td><%= HiddenSinglePrecent%>% (<%=HiddenSingleApplied%>) </td>
        </tr>
        <tr class="even">
            <td valign="top">Intersection Reduction</td><td><%= IntersectionReductionPrecent%>% (<%=IntersectionReductionApplied%>) </td>
        </tr>
        <tr class="even">
            <td valign="top">Naked Pairs</td><td><%= NakedPairsPrecent%>% (<%=NakedPairsApplied%>) </td>
        </tr>
        <tr class="even">
            <td valign="top">Hidden Pairs</td><td><%= HiddenPairsPrecent%>% (<%=HiddenPairsApplied%>) </td>
        </tr>

        <tr>
            <td class="header">Misconception : <br/> %(Number)</td><td colspan="2"><%=misconceptionPrecent%>% (<%=misconception%>)</td>
        </tr>

    </table>

    <%
        }
    %>

